const formatTime = (date, type) => {
  date = date.substring(0,date.length - 6);
  date = new Date(date)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return [year, month, day].map(formatNumber).join(type === 'data' ? '-' : '/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const liveStartTime = date => {
  date = new Date(date)
  var nowDate = new Date().getTime()
  var startDate = new Date(new Date(date.toLocaleDateString())).getTime()
  if ((date.getTime() - nowDate) > (date.getTime() - startDate) && (date.getTime() - nowDate) < (date.getTime() - startDate + 24 * 60 * 60 * 1000)) {
    return '明天' + (date.getHours() >= 10 ? date.getHours() : '0' + date.getHours()) + ':' + (date.getMinutes() >= 10 ? date.getMinutes() : '0' + date.getMinutes()) + '开始'
  } else if ((date.getTime() - nowDate) <= (date.getTime() - startDate)) {
    return '今天' + (date.getHours() >= 10 ? date.getHours() : '0' + date.getHours()) + ':' + (date.getMinutes() >= 10 ? date.getMinutes() : '0' + date.getMinutes()) + '开始'
  } else if ((date.getTime() - nowDate) >= (date.getTime() - startDate + 24 * 60 * 60 * 1000)) {
    return (date.getMonth() + 1) + '月' + date.getDate() + '日开始'
  };
}

const handleDate = date => {
  date = new Date(date);
  return date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate()
}

const splicing = src => {
  if (!new RegExp('https://').test(src) && !new RegExp('http://').test(src)) {
    return 'https://wxapp.bjeesa.cn/emigrate-api' + src
  } else {
    return src
  };
}

module.exports = {
  formatTime: formatTime,
  liveStartTime: liveStartTime,
  handleDate: handleDate,
  splicing: splicing
}